package com.noble.meether.chat.ui.system.store;

import com.noble.dbcenter.DBRequest;
import com.noble.dbcenter.entity.CollectionCallBack;
import com.noble.library.log.PLogger;
import com.noble.meether.chat.MessageConstant;
import com.noble.meether.chat.ui.system.bean.SysNotice;
import com.noble.meether.db.DBMgr;
import com.noble.meether.db.callbace.DBCallback;
import com.squareup.sqlbrite.BriteDatabase;

import java.util.List;

/**
 * 公告表
 */
public class DBNoticeCache {
    private BriteDatabase mDatabase;
    private DBMgr.DBHandler handler;

    public DBNoticeCache(BriteDatabase database, DBMgr.DBHandler handler) {
        this.mDatabase = database;
        this.handler = handler;
    }

    private void makeDBCallback(DBCallback callback, long ret) {
        if (callback != null) {
            callback.OnDBExecuted(ret >= 0 ? MessageConstant.OK : MessageConstant.ERROR);
        }
    }

    // ================================================= 插入 =============================================

    /**
     * 插入单条
     */
    public void insertMsg(final SysNotice sysNotice, final DBCallback callback) {
        handler.post(() -> {
            long result = insertLetter(sysNotice);
            makeDBCallback(callback, result);
        });
    }

    /**
     * 多条消息插入
     */
    public void insertMsgBatch(final List<SysNotice> list, final DBCallback callback) {
        handler.post(() -> {
            long ret = MessageConstant.OK;
            BriteDatabase.Transaction transaction = mDatabase.newTransaction();
            try {
                for (SysNotice item : list) {
                    ret = insertLetter(item);
                    if (ret != MessageConstant.OK) {
                        break;
                    }
                }
                transaction.markSuccessful();
            } finally {
                transaction.end();
            }
            makeDBCallback(callback, ret);
        });
    }

    private long insertLetter(final SysNotice sysNotice) {
        if (sysNotice == null) {
            return MessageConstant.ERROR;
        }

        try {
            long ret = mDatabase.insert(NoticeTable.TABLE_NAME, NoticeTable.toContentValues(sysNotice));
            return ret >= 0 ? MessageConstant.OK : MessageConstant.ERROR;
        } catch (Exception e) {
            e.printStackTrace();
            return MessageConstant.ERROR;
        }
    }

    // ================================================= 查询 =============================================

    /**
     * 查询消息记录
     */
    @SuppressWarnings("unchecked")
    public void queryMsgList(DBNoticeCallback queryCallback) {
        // .querySql("SELECT * FROM " + PeopleTable.TABLE_NAME+ " where name = ?")
        try {
            new DBRequest.Builder()
                    .tableName(NoticeTable.TABLE_NAME)
                    .querySql("SELECT * FROM " + NoticeTable.TABLE_NAME + ";")
                    .mapper(NoticeTable.MAPPER)
                    .getList(getSubscriber(queryCallback));
        }catch (Exception e) {
            PLogger.printThrowable(e);
        }
    }

    /**
     * @return 数据库查询回调
     */
    private CollectionCallBack<SysNotice> getSubscriber(final DBNoticeCallback queryCallback) {
        return new CollectionCallBack<SysNotice>() {
            @Override
            public void onFailure(int code, String message) {
                if (queryCallback != null)
                    queryCallback.onFailure(code, message);
            }

            @Override
            public void onCompleted() {
            }

            // 获取查询数据列表
            @Override
            public void onSuccess(List<SysNotice> data) {
                if (queryCallback != null)
                    queryCallback.onSuccess(data);
            }
        };
    }

    public interface DBNoticeCallback {

        void onFailure(int code, String message);

        void onSuccess(List<SysNotice> data);
    }

}
